package com.zhang.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhang.domain.Department;
import com.zhang.domain.Manager;
import com.zhang.domain.Shop;
import com.zhang.mapper.EmployeeMapper;
import com.zhang.mapper.ShopMapper;
import com.zhang.query.ShopQuery;
import com.zhang.service.IShopService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author zh
 * @create 2022-02-28 18:47
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class ShopServiceImpl implements IShopService {
    @Resource
    private ShopMapper mapper;
    @Resource
    private EmployeeMapper employeeMapper;
    @Transactional
    @Override
    public void save(Shop d) {
        if(d.getAdmin().getId()==null){
            employeeMapper.add(d.getAdmin());
            d.setLogo("http://34.80.47.178/"+d.getLogo());
            d.setRegisterTime(new Date());
        }
        mapper.save(d);
    }
    @Transactional
    @Override
    public void update(Shop d) {
        if(!d.getLogo().contains("http://34.80.47.178/")){
            d.setLogo("http://34.80.47.178/"+d.getLogo());
        }
        mapper.update(d);
    }
    @Transactional
    @Override
    public void delete(Long id) {
        mapper.delete(id);
    }

    @Override
    public PageInfo<Shop> queryData(ShopQuery query) {
        PageHelper.startPage(query.getCurrentPage(),query.getPageSize());
        List<Shop> shops = mapper.queryData(query);
        Page<Shop> page = (Page<Shop>) shops;
        return page.toPageInfo();
    }

    @Override
    public List<Manager> selAllEmp() {
        return mapper.selAllEmp();
    }
}
